﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ASPNETMVCApplication.ActionServiceReference;
using ASPNETMVCApplication.Repositories.Core;

namespace ASPNETMVCApplication.Repositories
{
    /// <summary>
    /// Order Repository.
    /// </summary>
    public class OrderRepository : RepositoryBase, IOrderRepository
    {
        /// <summary>
        /// Returns an order by orderId.
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public Order Get(int orderId)
        {
            //var request = new OrderRequest().Prepare();

            //request.LoadOptions = new string[] { "Order", "Customer", "OrderDetails" };
            //request.Criteria = new OrderCriteria { OrderId = orderId };

            //var response = Client.GetOrders(request);

            //Correlate(request, response);
            List<OrderDetail> OrderDetailList = new List<OrderDetail>();
            var request = new OrderRequest().Prepare();

            request.LoadOptions = new string[] { "Order" };
            request.Criteria = new OrderCriteria
            {
                OrderId = orderId,
                SortExpression = "ASC"
            };

            var response = Client.GetOrders(request);

            Correlate(request, response);

            Order order = response.Order;
            if (response.ChairList.Count() > 0) 
            { 
                for(int i = 0;i< response.ChairList.Count();i++)
                {
                    OrderDetail Detail = new OrderDetail();
                    Detail.CategoryId = response.CategoryId;                    
                    Detail.OrderId = orderId;
                    Detail.ProductId = response.ChairList[i];
                    Detail.ProductName = response.ChairNameList[i];
                    Detail.Quantity = 1;
                    Detail.Status = response.StatusList[i];
                    Detail.UnitPrice = response.PriceList[i];
                    Detail.OrderDetailId = response.OrderDetailIdList[i];
                    OrderDetailList.Add(Detail);
                }
            }
            
             if (request.RequestId != response.CorrelationId)
                throw new ApplicationException("GetOrder: RequestId and CorrelationId do not match.");
             order.OrderDetails = OrderDetailList.ToArray();
             return order;
        }

        public void InsertOrder(Order order, List<int> ChairList,List<string> ChairNameList, int CategoryId)
        {
            var request = new OrderRequest().Prepare();
            request.Action = "Create";
            request.Order = order;
            request.ChairList = ChairList.ToArray();
            request.ChairNameList = ChairNameList.ToArray();
            request.CategoryId = CategoryId;
            //request.OrderDetailList = OrderDetailList.ToArray();
            
            var response = Client.SetOrders(request);

            Correlate(request, response);

            // These messages are for public consumption. Includes validation errors.
            if (response.Acknowledge == AcknowledgeType.Failure)
                throw new ApplicationException(response.Message);
            
        }

        public void Insert(Order order)
        {

            var request = new OrderRequest().Prepare();
            request.Action = "Create";
            request.Order = order;
            //request.ChairList = ChairList.ToArray();

            var response = Client.SetOrders(request);

            Correlate(request, response);

            // These messages are for public consumption. Includes validation errors.
            if (response.Acknowledge == AcknowledgeType.Failure)
                throw new ApplicationException(response.Message);

        }       

        public List<Order> GetOrderList(int CustId,int SuppId, DateTime DateFrom, DateTime DateThru, int Status)
        {
            var request = new OrderRequest().Prepare();

            request.LoadOptions = new string[] { "Orders" };
            request.Criteria = new OrderCriteria
            {                
                SupplierId = SuppId,
                DateFrom = DateFrom,
                DateThru = DateThru,
                Status = Status,
                CustomerId = CustId,
                SortExpression = "ASC"
            };

            var response = Client.GetOrders(request);

            Correlate(request, response);

            return response.Orders == null ? null : response.Orders.ToList();
        }

        public void Update(Order order)
        {
            var request = new OrderRequest().Prepare();
            request.Action = "Update";
            request.Order = order;
            request.ChairNameList = null;
            var response = Client.SetOrders(request);

            Correlate(request, response);

            // These messages are for public consumption. Includes validation errors.
            if (response.Acknowledge == AcknowledgeType.Failure)
                throw new ApplicationException(response.Message);
        }

        public void UpdateOrder(Order order, List<int> ChairList)
        {
            var request = new OrderRequest().Prepare();
            request.Action = "Update";
            request.Order = order;
            request.ChairList = ChairList.ToArray();
            var response = Client.SetOrders(request);

            Correlate(request, response);

            // These messages are for public consumption. Includes validation errors.
            if (response.Acknowledge == AcknowledgeType.Failure)
                throw new ApplicationException(response.Message);
        }

        public void Delete(int orderId)
        {
            var request = new OrderRequest().Prepare();
            request.Action = "Delete";
            request.Order = new Order { OrderId = orderId };

            var response = Client.SetOrders(request);

            Correlate(request, response);
        }

        #region Not implemented members

        public List<ActionServiceReference.Order> GetList(Criterion criterion = null)
        {
            throw new NotImplementedException();
        }

        public ActionServiceReference.Order Get(Criterion criterion = null)
        {
            throw new NotImplementedException();
        }

        public int GetCount(Criterion criterion = null)
        {
            throw new NotImplementedException();
        }


        #endregion
    }
}